﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace orders
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            MaximizeBox = false;
        }
        private Int32 ID = -1;

        private MotabaaEntity fillMotabaaEntityFromForm()
        {
            MotabaaEntity motabaa = new MotabaaEntity();
            motabaa.Today = cmbotoday.SelectedItem.ToString();
            motabaa.Dates = pkrdate.Value;
            motabaa.NameOfBox = txtnamebox.Text;
            motabaa.hieght = txthieght.Text;
            motabaa.width = txtwidth.Text;
            motabaa.CountOfPaper = txtcount.Text;
            motabaa.OrderOfPrint = chkorderprint.Checked;
            motabaa.ServiceAfterPrint = chkserviceafterprint.Checked;
            motabaa.Cartoon = chkcartoon.Checked;
            motabaa.Notes = txtnotes.Text;
            motabaa.Name_Estamba = txtnameestambaa.Text;
            if(chkcartoon.Checked)
            {
            motabaa.Date_Cartoon = pkrcartoon.Value;
            }
          
            
           
            return motabaa;
        }
        private void PublishMotabaaValueToForm(MotabaaEntity mo)
        {
            ID = mo.Id;
            cmbotoday.Text = mo.Today;
            pkrdate.Value = mo.Dates;
            txtnamebox.Text = mo.NameOfBox;
            txthieght.Text = mo.hieght;
            txtwidth.Text = mo.width;
            txtcount.Text = mo.CountOfPaper;
            chkorderprint.Checked = mo.OrderOfPrint;
            chkserviceafterprint.Checked = mo.ServiceAfterPrint;
            chkcartoon.Checked = mo.Cartoon;
            txtnotes.Text = mo.Notes;
            txtnameestambaa.Text = mo.Name_Estamba;
            pkrcartoon.Value = mo.Date_Cartoon;


        }
        private void clearAllFields()
        {
            cmbotoday.Text = null;
            pkrdate.Value = DateTime.Today;
            txtnamebox.Text = "";
            txthieght.Text = "";
            txtwidth.Text = "";
            txtcount.Text = "";
            chkorderprint.Checked = false;
            chkserviceafterprint.Checked = false;
            chkcartoon.Checked = false;
            txtnotes.Text = "";
            txtnameestambaa.Text = "";
            pkrcartoon.Value = DateTime.Today;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmbotoday.Text == "" || txtnamebox.Text.Equals(null) || txthieght.Text == "" || txtwidth.Equals(null))
                MessageBox.Show("يجب إدخال كل البيانات");
            else
            {
                
                MotabaaEntity motabaa = fillMotabaaEntityFromForm();
                if (Motabaa.insert(motabaa))
                {
                    MessageBox.Show("تم حفظ البيانات بنجاح");
                    Showgridview(Motabaa.GetByNamebox("%"));

                }
                clearAllFields();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            clearAllFields();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ID != -1)
            {
                DialogResult result = MessageBox.Show("هل تريد حذف " + txtnamebox.Text + "؟ ", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    if (Motabaa.delete(ID))
                    {
                        MessageBox.Show("تم حذف العلبه بنجاح");
                        clearAllFields();
                        Showgridview(Motabaa.GetByNamebox("%"));
                        button2.Enabled = false;
                        btnedit.Enabled = false;
                    }
                    else
                        MessageBox.Show("حدث خطأ فى الحذف");
                }
            }
            else
                MessageBox.Show("بعض البيانات غبر موجودة");
        }
        private void Showgridview(List<MotabaaEntity> d)
        {
            gridview.DataSource = d;



        }

        private void txtnamebox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                List<MotabaaEntity> m = Motabaa.GetByNamebox("%" + txtnamebox.Text + "%");
                if (m.Count == 0)
                {
                    MessageBox.Show(" عذراً ! هذه العلبة غير موجوده ");
                }

                else
                {
                    Showgridview(m);
                }
            }
        }
        public void publishmemberfromgrid(int Row)
        {
            ID = Convert.ToInt32(gridview.Rows[Row].Cells["motabaa_id"].Value.ToString());
            cmbotoday.Text = gridview.Rows[Row].Cells["Today"].Value.ToString();
            pkrdate.Value = Convert.ToDateTime(gridview.Rows[Row].Cells["Dates"].Value.ToString());
            txtnamebox.Text = gridview.Rows[Row].Cells["NameOfBox"].Value.ToString();
            txthieght.Text = gridview.Rows[Row].Cells["hieght"].Value.ToString();
            txtwidth.Text = gridview.Rows[Row].Cells["width"].Value.ToString();
            txtcount.Text = gridview.Rows[Row].Cells["CountOfPaper"].Value.ToString();
            chkorderprint.Checked = Convert.ToBoolean(gridview.Rows[Row].Cells["OrderOfPrint"].Value.ToString());
            chkserviceafterprint.Checked = Convert.ToBoolean(gridview.Rows[Row].Cells["ServiceAfterPrint"].Value.ToString());
            chkcartoon.Checked = Convert.ToBoolean(gridview.Rows[Row].Cells["Cartoon"].Value.ToString());
            txtnotes.Text = gridview.Rows[Row].Cells["Notes"].Value.ToString();
            if (pkrcartoon.Value==null)
            {
              Convert.ToDateTime(gridview.Rows[Row].Cells["Date_Cartoon"].Value.ToString());   
            }
            txtnameestambaa.Text = gridview.Rows[Row].Cells["Name_Estamba"].Value.ToString();

        }

        private void gridview_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            publishmemberfromgrid(e.RowIndex);
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
           if (cmbotoday.Text == "" || txtnamebox.Text.Equals(null) || txthieght.Text == "" || txtwidth.Equals(null))
                MessageBox.Show(" عفواً ! يجب إختيار البيان المطلوب تعديله");
            else
              {
            MotabaaEntity motabaa = fillMotabaaEntityFromForm();
            if (ID > 0)
            {
                motabaa.Id = ID;
                Motabaa.Update(motabaa);
                MessageBox.Show("تم تعديل البيانات بنجاح");
                clearAllFields();
                ID = 0;
                Showgridview(Motabaa.GetByNamebox("%"));
            }
            }
        }

        private void txtnotes_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
            {
                List<MotabaaEntity> m = Motabaa.GetByNotes("%" + txtnotes.Text + "%");
                if (m.Count == 0)
                {
                    MessageBox.Show("هذه الملاحظة غير موجوده");
                }

                else
                {
                    Showgridview(m);
                }
            }

        }

        private void btnrestore_Click(object sender, EventArgs e)
        {
            clearAllFields();
            Showgridview(Motabaa.GetByNamebox("%"));
        }
    }
}

